package exc;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * 
 * @author XIAOBAI123
 *
 */
public class StringReplaceString01 {
	List<String> des = new ArrayList<String>();
	List<String> sou = new ArrayList<String>();
	List<String> my = new ArrayList<String>();
	List<String> yuan = new ArrayList<String>();
	String start = null;
	String end = null;
	String startSou = null;
	String endSou = null;

	public static void main(String[] args) {
		StringReplaceString01 srs = new StringReplaceString01();
		srs.readfile();
		srs.replaceString(srs.start, srs.end, srs.startSou, srs.endSou);
		srs.writeString();
	}

	private void readfile() {
		File file1 = new File("F:/截取.txt");
		File file2 = new File("F:/替换.txt");
		File file3 = new File("F:/源.txt");
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file1));
			while (br.ready()) {
				String s = br.readLine();
				des.add(s);
			}

			br = new BufferedReader(new FileReader(file2));
			while (br.ready()) {
				String s = br.readLine();
				sou.add(s);
			}
			
			br = new BufferedReader(new FileReader(file3));
			while(br.ready()){
				yuan.add(br.readLine());
			}

			start = yuan.get(0);
			end = yuan.get(1);
			startSou = yuan.get(2);
			endSou = yuan.get(3);
			System.out.println(start);
			System.out.println(end);
			System.out.println(startSou);
			System.out.println(endSou);
			
		} catch (IOException e) {
			System.out.println("replace err...");
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	private void replaceString(String startDes, String endDes, String startSou,
			String endSou) {
		if (des.size() != 0 && sou.size() != 0) {
			for (int i = 0; i < des.size(); i++) {
				System.out.println(des.get(i).startsWith(startSou));
				System.out.println(des.get(i).endsWith(endSou));
				if (des.get(i).startsWith(startSou)) {
					String[] start = des.get(i).split(startSou);
//					for (int j = 0; j < start.length; j++) {
						String[] end = start[1].split(endSou);
						if (sou.get(i).startsWith(startDes) && sou.get(i).endsWith(endDes)) {
//							for (int x = 0; x < end.length; x++) {
								if(end[1] != null){
									my.add(startDes + end[0].trim() + endDes);
								}
							}
						}
//					}
//				}
			}
		}
	}

	private void writeString() {
		File file = new File("F:/替换后的文本.txt");
		if (file.exists()) {
			file.delete();
			file = new File("F:/替换后的文本.txt");
		}
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(file));
			for (int i = 0; i < my.size(); i++) {
				bw.write(my.get(i));
				bw.write("\r");
				bw.write("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
